package com.designoutput.utils
{
	import flash.geom.Point;

	public class SnapUtils
	{
		public static var snapEnabled:Boolean = false;
		public static var m_nMouseLock:Number = 0;
		
		public static function getSnapRect(nX1:Number, nY1:Number, nX2:Number, nY2:Number):Point
		{
			var obj:Point = new Point();
			var dx:Number = nX2 - nX1;
			var dy:Number = nY2 - nY1;
			var nMinSize:Number = Math.min(Math.abs(dx), Math.abs(dy));
			obj.x = nX1 + nMinSize * (dx > 0 ? 1 : -1);
			obj.y = nY1 + nMinSize * (dy > 0 ? 1 : -1);
			return obj;
		}
		
		public static function getSnapLine(nX1:Number, nY1:Number, nX2:Number, nY2:Number):Point
		{
			var obj:Point = new Point();
			var dx:Number = nX2 - nX1;
			var dy:Number = nY2 - nY1;
			var nOldAlpha:Number = dx != 0 ? Math.atan(dy / dx) * 180 / Math.PI : (dy > 0 ? 90 : -90);
			if(dx < 0) nOldAlpha += 180;
			var nLen:Number = Math.sqrt(dx * dx + dy * dy);
			var nNewAlpha:Number = Math.round(nOldAlpha / 15) * 15;
			obj.x = nX1 + nLen * Math.cos(nNewAlpha * Math.PI / 180);
			obj.y = nY1 + nLen * Math.sin(nNewAlpha * Math.PI / 180);
			return obj;
		}
	}
}